<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;
use app\common\controller\AdminBaseController;

class Accounts extends AdminBaseController
{
	public function cz_glj(){
		if($_POST){
			if(!input("price")||!input("op_pwd")){
				return json(['code'=>402,'msg'=>'请输入正确的鼓励金金额和操作密码']);
			}
			$price=input("price");
			$admin=db("tg_auth_user")->where("id",session('admin_id'))->find();
			if(md5_salt(input("op_pwd"))!=$admin['op_pwd']){
				return json(['code'=>402,'msg'=>'操作密码不正确']);
			}	
			$result = true;
			Db::startTrans();
			$oem_id=input("oem_id");
			//增加oem鼓励金
			$rest1 = db('info_oem')->where('id', $oem_id)->setInc('score', $price);
			if(!$rest1)$result = false;
			$rest1 =oem_score_change(8,$price,'系统充值鼓励金'.$price,$oem_id,0,0);
			if(!$rest1)$result = false;
			$rest1 = Db::table('sys_tongji')->where('id', 1)->setInc('balances', $price/10);
			if(!$rest1)$result = false;
			$rest1 =$this->add_log($oem_id,$price);
			if(!$rest1)$result = false;
			if($result){
			    Db::commit();  
				return json(['code'=>400,'msg'=>'充值成功']);  
			}else{
			    Db::rollback();
				return json(['code'=>402,'msg'=>'充值失败']);
			}
		}
			$this->assign('oem_id',input("oem_id"));
			return $this->fetch();
	}	
	//添加充值日志
	public function add_log($oem_id,$price){
		$data['oem_id']=$oem_id;
		$data['message']="系统充值鼓励金";			
		$data['order_type']=9;
		$data['order_sn']=order_create_no();
		$data['pay_type']=1;
		$data['state']=2;
		$data['origin_price']=$price/10;
		$data['balance']=$price/10;
		$data['add_time']=now_time();
		$data['pay_time']=now_time();
		$res=db('log_paylog')->insert($data);
		$res1=db('log_paylog_copy')->insert($data);
		if(!$res||!$res1){
			return false;
		}
		return true;
	}
	public function fukuan(){
		$agent_info = db('info_agent')->where('id',input("agent_id"))->find();
    	$agent_store = db('agent_store')->where('agent_id',input('agent_id'))->find();
    	
    	$code_url="http://".$_SERVER['HTTP_HOST']."/index/pay/qrcode_pay?agent_id=".$agent_info['id']."&store_id=".$agent_store['id']."&clerk_id=0";
    	
    	$agent_info['code_url'] = $code_url;
    	$agent_info['agent_store_name'] = $agent_store['store_name'];
    	
    	$this->assign('agent_info',$agent_info);
		return $this->fetch();
	}	
	//OEM帐号管理
    public function oem_list()
    {
    	$data=Db::table('info_oem')->where('user_type=1 and user_level=1')->select();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    public function oem_add()
    {
    	if($_POST){
	        $parm['mobile']=$_POST['mobile'];
	        $parm['password']=md5_salt($_POST['password']);
	        $parm['addres']=$_POST['addres'];
	        $parm['app_key']=$_POST['app_key'];
	        $parm['app_secret']=$_POST['app_secret'];
	        $parm['project_name']=$_POST['project_name'];
	        $parm['web_url']=$_POST['web_url'];
	        $parm['user_name']=$_POST['project_name'];
	        $parm['user_type']=1;
	        $parm['user_level']=1;
	        $parm['add_time']=now_time();
	        
	        $result=Db::table('info_oem')->insert($parm);
	        if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
    	return $this->fetch();
    }
    public function oem_edit()
    {
    	if($_POST){
    		$parm['id']=$_POST['id'];
	        $parm['mobile']=$_POST['mobile'];
	        $parm['password']=md5_salt($_POST['password']);
	        $parm['addres']=$_POST['addres'];
	        $parm['app_key']=$_POST['app_key'];
	        $parm['app_secret']=$_POST['app_secret'];
	        $parm['project_name']=$_POST['project_name'];
	        $parm['web_url']=$_POST['web_url'];
	        $parm['user_name']=$_POST['project_name'];
	        $result=Db::table('info_oem')->where('id',input('id'))->update($parm);
	        if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
        $data=Db::table('info_oem')->where('id',input('id'))->find();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    
    //省代帐号管理
    public function agency_id1()
    {
    	$data=Db::table('info_oem a')->
    	join("info_oem b","a.oem_id=b.id",'LEFT')->
    	where('a.user_type=1 and a.user_level=2')->
    	field("a.*,b.user_name as oname")->
    	select();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    
    //市代帐号管理
    public function agency_id2()
    {
    	$data=Db::table('info_oem a')->
    	join("info_oem b","a.agency_id=b.id",'LEFT')->
    	where('a.user_type=1 and a.user_level=3')->
    	field("a.*,b.user_name as pname")->
    	select();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    
    
    //会员列表
    public function user_list()
    {
    	$where = '';
    	if(input('mobile') && input('mobile')>0){
			$where['a.mobile'] = input('mobile');
		}
		
    	$list=Db::table('info_user a ')->
    	join("info_oem d","a.oem_id=d.id",'LEFT')->
    	join("info_oem b","a.agency_id2=b.id",'LEFT')->
		join("info_oem c","a.agency_id1=c.id",'LEFT')->
		join("info_agent e","a.tj_agent=e.id",'LEFT')->
		join("info_user f","a.level=f.id",'LEFT')->
    	field("a.*,b.user_name as pname,c.user_name as cname,d.user_name as dname,e.agent_name,f.mobile as tmobile")->
    	where($where)->
    	order('add_time desc')->paginate(20);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    //商家列表
    public function agent_list()
    {
    	
		if(input('mobile') && input('mobile')>0){
			$where['a.mobile'] = ['like','%'.input('mobile').'%'];
		}
		
		if(input('tel') && input('tel')>0){
			$where['a.tel'] = ['like','%'.input('tel').'%'];
		}
		
		if(input('is_state') && input('is_state')>0){
			$where['a.is_state'] = input('is_state');
		}
    	
    	$where['a.id'] = ['>',0];
//		$where['a.status'] = 1;	
    	$list=Db::table('info_agent a')->
		join("info_oem b","a.agency_id2=b.id",'LEFT')->
		join("info_oem c","a.agency_id1=c.id",'LEFT')->
		where($where)->
    	field("a.*,b.user_name as pname,c.user_name as cname")->
    	order('add_time desc')
    	->paginate(20,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    //银行签约
//  public function agent_nh()
//  {
//  	$this->assign('id',input('id'));
//  	$data=Db::table('info_agent')->find(input('id'));
//  	$this->assign('data',$data);
//  	if($_POST){
//	        $parm['cb_miyao']		= $_POST['cb_miyao'];
//	        $parm['cb_no']			= $_POST['cb_no'];
//	        $parm['cb_clerk']		= $_POST['cb_clerk'];
//	        $parm['cb_huilu']		= $_POST['cb_huilu'];
//	        $parm['password']		= md5_salt($_POST['password']);
//	        $parm['cb_login_name']	= $_POST['cb_login_name'];
//	        $result=Db::table('info_agent')->where('id',input('id'))->update($parm);
//	        if($result){
//				return json(['code'=>200,'msg'=>'成功']);
//	        }else{
//	       		return json(['code'=>402,'msg'=>'失败']);
//	        }
//      }
//  	return $this->fetch();
//  }
    //官方签约
    public function agent_gf()
    {
    	$this->assign('id',input('id'));
    	$data=Db::table('info_agent')->find(input('id'));
    	$this->assign('data',$data);
    	if($_POST){
	        $parm['ali_token']=$_POST['ali_token'];
	        $parm['ali_sellid']=$_POST['ali_sellid'];
	        $result=Db::table('info_agent')->where('id',input('id'))->update($parm);
	        if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
    	return $this->fetch();
    }
	
	//省代市代冻结激活
	public function complain_frozen(){
		if($_GET['status']==1){
			$data['status'] = 2;
		}else{
			$data['status'] = 1;
		}    
	    $result=Db::table('info_oem')->where('id',$_GET['id'])->update($data);
        if($result){
			 return json(['code'=>200,'msg'=>'成功']);
       	}else{
       		return json(['code'=>402,'msg'=>'失败']);
       	}
		
		$data=Db::table('info_oem')->where('user_type=1 and user_level=3')->select();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
	
	//会员冻结激活
	public function user_frozen(){
		if($_GET['status']==1){
			$data['status'] = 2;
		}else{
			$data['status'] = 1;
		}    
	    $result=Db::table('info_user')->where('id',$_GET['id'])->update($data);
        if($result){
			 return json(['code'=>200,'msg'=>'成功']);
       	}else{
       		return json(['code'=>402,'msg'=>'失败']);
       	}
		
//  	$list=Db::table('info_user')->order('add_time desc')->paginate(5);
//  	$this->assign('list',$list);
    	return $this->fetch();
    }
	
	//商家冻结激活
	public function agent_frozen(){
		if($_GET['status']==1){
			$data['status'] = 2;
		}else{
			$data['status'] = 1;
		}    
	    $result=Db::table('info_agent')->where('id',$_GET['id'])->update($data);
        if($result){
			 return json(['code'=>200,'msg'=>'成功']);
       	}else{
       		return json(['code'=>402,'msg'=>'失败']);
       	}

    	return $this->fetch();
    }
	
	//OEM余额记录
    public function oemye()
    {
    	$where['oem_id'] = input('oem_id');
    	//余额
    	$sums=db('log_oem_charge')->where($where)->sum("balance");
    	$this->assign('sums',$sums);
		
		//累积收入
		$sumsr=db('log_oem_charge')->where('balance','NOT LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumsr',$sumsr);
		
		//累积支出
		$sumzc=db('log_oem_charge')->where('balance','LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumzc',$sumzc);
		
    	$list = Db::table('log_oem_charge')
    	->where($where)
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->paginate(15);
    	$this->assign('list',$list);
		$this->assign('type',input('type'));
		$this->assign('oem_id',input('oem_id'));
    	return $this->fetch();
    }
	
	//oem余额记录导出
    public function oemye_dc(){		
		$list = Db::table('log_oem_charge')
    	->where('oem_id',input('oem_id'))
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
		->select();

		$str ="操作类型,金额,信息,增加时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$typename='';
        	if($row['order_type']==1){
        		$typename = '提现';
        	}elseif($row['order_type']==2){
        		$typename = '佣金转余额';
        	}elseif($row['order_type']==3){
        		$typename = '分佣';
        	}elseif($row['order_type']==4){
        		$typename = '鼓励金转余额';
        	}
        	$typename = iconv('utf-8','gbk',$typename.'	');
			$message = iconv('utf-8','gbk',$row['message']); 
			$balance = iconv('utf-8','gbk',$row['balance']); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $typename.",".$balance.",".$message.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='OEM余额记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".iconv('utf-8','gbk',$filename)); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//OEM佣金记录
    public function oemyj()
    {
    	$list = Db::table('log_oem_commission')
    	->where('oem_id',input('oem_id'))
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->paginate(15);
    	$this->assign('list',$list);
		$this->assign('type',input('type'));
    	return $this->fetch();
    }
	
	//OEM鼓励金记录
    public function oemglj()
    {
    	$where['oem_id'] = input('oem_id');
    	//余额
    	$sums=db('log_oem_score')->where($where)->sum("balance");
    	$this->assign('sums',$sums);
		
		//累积收入
		$sumsr=db('log_oem_score')->where('balance','NOT LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumsr',$sumsr);
		
		//累积支出
		$sumzc=db('log_oem_score')->where('balance','LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumzc',$sumzc);

    	$list = Db::table('log_oem_score')
    	->where($where)
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->paginate(15);
    	$this->assign('list',$list);
		$this->assign('oem_id',input('oem_id'));
		$this->assign('type',input('type'));
    	return $this->fetch();
    }
    
    //oem鼓励金记录导出
    public function oemglj_dc(){
    	$list = Db::table('log_oem_score')
    	->where('oem_id',input('oem_id'))
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->select();

		$str ="操作类型,金额,信息,增加时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$typename='';
        	if($row['order_type']==1){
        		$typename = '提现';
        	}elseif($row['order_type']==2){
        		$typename = '佣金转余额';
        	}elseif($row['order_type']==3){
        		$typename = '分佣';
        	}elseif($row['order_type']==4){
        		$typename = '鼓励金兑现';
        	}elseif($row['order_type']==5){
        		$typename = '鼓励金充值';
        	}elseif($row['order_type']==6){
        		$typename = '鼓励金收款';
        	}elseif($row['order_type']==7){
        		$typename = '鼓励金分佣';
        	}elseif($row['order_type']==8){
        		$typename = '鼓励金转账分佣';
        	}
        	$typename = iconv('utf-8','gbk',$typename.'	');
			$message = iconv('utf-8','gbk',$row['message']); 
			$balance = iconv('utf-8','gbk',$row['balance']); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $typename.",".$balance.",".$message.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='OEM鼓励金记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".iconv('utf-8','gbk',$filename)); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//商家余额记录
    public function agentye()
    {
    	$where['agent_id'] = input("agent_id");
    	//余额
    	$sums=db('log_agent_charge')->where($where)->sum("balance");
    	$this->assign('sums',$sums);

		//累积收入
		$sumsr=db('log_agent_charge')->where('balance','NOT LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumsr',$sumsr);
		
		//累积支出
		$sumzc=db('log_agent_charge')->where('balance','LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumzc',$sumzc);
    	$list = Db::table('log_agent_charge')
    	->where($where)
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->paginate(15);
    	$this->assign('list',$list);
		$this->assign('agent_id',input('agent_id'));
    	return $this->fetch();
    }
	
	//商家余额记录导出
    public function agentye_dc(){
    	$list = Db::table('log_agent_charge')
    	->where('agent_id',input('agent_id'))
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->select();

		$str ="操作类型,金额,信息,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	if($row['order_type']==1){
        		$typename = '订单';
        	}elseif($row['order_type']==2){
        		$typename = '订单退款';
        	}elseif($row['order_type']==3){
        		$typename = '扫码支付';
        	}elseif($row['order_type']==4){
        		$typename = '扫码退款';
        	}elseif($row['order_type']==5){
        		$typename = '提现';
        	}elseif($row['order_type']==6){
        		$typename = '兑现积分';
        	}elseif($row['order_type']==7){
        		$typename = '充值广告费';
        	}elseif($row['order_type']==8){
        		$typename = '扣广告费';
        	}elseif($row['order_type']==9){
        		$typename = '线下收银';
        	}elseif($row['order_type']==10){
        		$typename = '提现失败';
        	}
        	$typename = iconv('utf-8','gbk',$typename.'	');
			$message = iconv('utf-8','gbk',$row['message']); 
			$balance = iconv('utf-8','gbk',$row['balance']); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $typename.",".$balance.",".$message.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='商家余额记录.csv';   //设置文件名  
	  	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".iconv('utf-8','gbk',$filename)); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//商家鼓励金记录
    public function agentglj()
    {
    	$where['agent_id'] = input('agent_id');
    	//余额
    	$sums=db('log_agent_score')->where($where)->sum("balance");
    	$this->assign('sums',$sums);
		
		//累积收入
		$sumsr=db('log_agent_score')->where('balance','NOT LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumsr',$sumsr);
		
		//累积支出
		$sumzc=db('log_agent_score')->where('balance','LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumzc',$sumzc);
    	$list = Db::table('log_agent_score')
    	->where($where)
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->paginate(15);
    	$this->assign('list',$list);
		$this->assign('agent_id',input('agent_id'));
    	return $this->fetch();
    }
	
	//商家鼓励金记录导出
    public function agentglj_dc(){
    	$list = Db::table('log_agent_score')
    	->where('agent_id',input('agent_id'))
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->select();

		$str ="操作类型,金额,信息,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	if($row['order_type']==1){
        		$typename = '订单';
        	}elseif($row['order_type']==2){
        		$typename = '订单退款';
        	}elseif($row['order_type']==3){
        		$typename = '扫码支付';
        	}elseif($row['order_type']==4){
        		$typename = '扫码退款';
        	}elseif($row['order_type']==5){
        		$typename = '鼓励金充值';
        	}elseif($row['order_type']==6){
        		$typename = '鼓励金兑现';
        	}elseif($row['order_type']==7){
        		$typename = '线下收银';
        	}elseif($row['order_type']==8){
        		$typename = '提前分佣';
        	}elseif($row['order_type']==9){
        		$typename = '我的会员充值分佣';
        	}elseif($row['order_type']==10){
        		$typename = '发红包';
        	}elseif($row['order_type']==11){
        		$typename = '红包退还';
        	}
        	$typename = iconv('utf-8','gbk',$typename.'	');
			$message = iconv('utf-8','gbk',$row['message']); 
			$balance = iconv('utf-8','gbk',$row['balance']); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $typename.",".$balance.",".$message.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='商家鼓励金记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".iconv('utf-8','gbk',$filename)); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//OEM认证资料
	public function renzhenginfo(){
		$account=db('sys_member_verif')
		->field('user_name,id_number,bank_name,bank_no,reserve_mobile,id_card_front,half_photo,bank_card_front,id_card_behind')
		->where('user_id',input("id"))
		->where('user_type',2)
		->find();
		$this->assign('data',$account);
		return $this->fetch();
	}
	
	//个人余额记录
    public function userye(){
    	$where['u_id'] = input('user_id');
    	//余额
    	$sums=db('log_user_charge')->where($where)->sum("balance");
    	$this->assign('sums',$sums);
		
		//累积收入
		$sumsr=db('log_user_charge')->where('balance','NOT LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumsr',$sumsr);
		
		//累积支出
		$sumzc=db('log_user_charge')->where('balance','LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumzc',$sumzc);
		  
    	$list = Db::table('log_user_charge')
    	->where($where)
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->paginate(15);
    	$this->assign('list',$list);
		$this->assign('user_id',input("user_id")); 
    	return $this->fetch();
    }

	//会员余额记录导出
    public function userye_dc(){
    	$list = Db::table('log_user_charge')
    	->where('u_id',input('user_id'))
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
		->select();
		
		$str ="操作类型,金额,信息,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	if($row['order_type']==1){
        		$typename = '提现';
        	}elseif($row['order_type']==2){
        		$typename = '佣金转余额';
        	}elseif($row['order_type']==3){
        		$typename = '广告充值';
        	}elseif($row['order_type']==4){
        		$typename = '鼓励金兑现';
        	}elseif($row['order_type']==5){
        		$typename = '二维码付款';
        	}
        	$typename = iconv('utf-8','gbk',$typename.'	');
			$message = iconv('utf-8','gbk',$row['message']); 
			$balance = iconv('utf-8','gbk',$row['balance']); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $typename.",".$balance.",".$message.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='会员余额记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".iconv('utf-8','gbk',$filename)); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//个人佣金记录
    public function useryj(){  
    	$list = Db::table('log_user_commission')
    	->where('u_id',input('user_id'))
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->paginate(15);
    	$this->assign('list',$list);
    	return $this->fetch();
    }

	//个人鼓励金记录
    public function userglj(){
		$where['u_id'] = input('user_id');
    	//余额
    	$sums=db('log_user_score')->where($where)->sum("balance");
    	$this->assign('sums',$sums);
		
		//累积收入
		$sumsr=db('log_user_score')->where('balance','NOT LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumsr',$sumsr);
		
		//累积支出
		$sumzc=db('log_user_score')->where('balance','LIKE','-%')->where($where)->sum('balance');
    	$this->assign('sumzc',$sumzc);
    	$list = Db::table('log_user_score')
    	->where($where)
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->paginate(15);
    	$this->assign('list',$list);
		$this->assign('user_id',input("user_id")); 
    	return $this->fetch();
    }
    
    
    //个人鼓励金记录导出
    public function userglj_dc(){
    	$list = Db::table('log_user_score')
    	->where('u_id',input('user_id'))
    	->field("order_type,balance,message,add_time")
		->order('add_time desc')
    	->select();

		$str ="操作类型,金额,信息,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	if($row['order_type']==1){
        		$typename = '订单佣金';
        	}elseif($row['order_type']==2){
        		$typename = '扫码佣金';
        	}elseif($row['order_type']==3){
        		$typename = '退款';
        	}elseif($row['order_type']==4){
        		$typename = '订单支付积分';
        	}elseif($row['order_type']==5){
        		$typename = '扫码支付积分';
        	}elseif($row['order_type']==6){
        		$typename = '线下收银';
        	}elseif($row['order_type']==7){
        		$typename = '充值';
        	}elseif($row['order_type']==8){
        		$typename = '个人转账';
        	}elseif($row['order_type']==9){
        		$typename = '兑现';
        	}elseif($row['order_type']==10){
        		$typename = '下级会员充值分佣';
        	}elseif($row['order_type']==11){
        		$typename = '上级转账';
        	}else{
        		$typename = '扫码佣金';
        	}
        	$typename = iconv('utf-8','gbk',$typename.'	');
			$message = iconv('utf-8','gbk',$row['message']); 
			$balance = iconv('utf-8','gbk',$row['balance']); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $typename.",".$balance.",".$message.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='用户鼓励金记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".iconv('utf-8','gbk',$filename)); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
    
}
